package georeduy.dao;

import java.util.List;

import georeduy.clases.Evento;
import georeduy.dao.interfaces.IEventoDAO;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;



@Stateless(name = "EventoDAO", mappedName = "GeoRed.uy-GeoRed.uy-JPA-EventoDAO")
public class EventoDAO
        implements  IEventoDAO
{
    @PersistenceContext(unitName="Geored.uy-JPA")
    private EntityManager em;

    public EventoDAO() { }

    public Object queryByRange(String jpqlStmt, int firstResult,
                               int maxResults) {
        Query query = em.createQuery(jpqlStmt);
        if (firstResult > 0) {
            query = query.setFirstResult(firstResult);
        }
        if (maxResults > 0) {
            query = query.setMaxResults(maxResults);
        }
        return query.getResultList();
    }

    public Evento persistEvento(Evento evento)
    { 
    	em.persist(evento); 
    	return evento;
    }


    public Evento mergeEvento(Evento evento) {return em.merge(evento);}

    public void removeEvento(Evento evento) {
        evento = em.find(Evento.class, evento.getId());
        em.remove(evento);
    }
    
    public void removeEvento(int id) {
        Evento evento = em.find(Evento.class, id);
        em.remove(evento);
    }

	@Override
	public Evento findEvento(int id) {
		/*String FINDEV = "select e from Evento e where e.id = :id  ";
		Query query = em.createQuery(FINDEV);
		query.setParameter("nombre", nombre);
		return (Evento)query.getSingleResult();*/
		return em.find(Evento.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Evento> ListarEventos() {
		 Query query = em.createQuery("select e from Evento e");
		 return query.getResultList();
	}




}
